/**
 * Created by gongxun on 16/12/27.
 */

var KwxGiftLayer = KwxSecondBase.extend({
    ListView_content : null,
    mMsgIds : null,
    ctor : function () {
        this._super();
        this.loadSecondCCS("res/ccs/KwxGiftLayer.json");
        this.setTitleText("res/ccs/hall_second/text/text_libao.png");
        this.mMsgIds = [];

        this.ListView_content = mo.seekWidgetByName(this.ccsNode, "ListView_content");
        this.ListView_content.setScrollBarEnabled(false);

        //加载模版
        this.loadItemTemple();

        mo.showWaitLayer(5);

        //修复剪切bug
        mo.gameHelper.setClippingLayout(this.ListView_content);
    },

    onEnter : function(){
        this._super();

        //找服务器拉消息
        mo.gameMsgManager.sendRedPkgWeiXingRequired();

        //拉消息通知
        mo.eventHelper.add("UserMessageIdList", this, function(){
            //loadMsg
            this.mMsgIds = mo.userMessage.msgIds.slice(0);
            this.updateListView();
            this.updateShowIsNull();
            mo.closeWaitLayer();
        });

        if (!mo.userMessage.getUserMessageIds()){
            //没有去啦读本地的
            this.mMsgIds = mo.userMessage.msgIds.slice(0);
            this.updateListView();
            this.updateShowIsNull();
            mo.closeWaitLayer();
        }

        mo.eventHelper.add("UserMessageResponse", this, function(userMessage){
            this.setItemInfo(userMessage.msg_id, userMessage);
        });

        mo.eventHelper.add("UserMessageAction", this, function(msgid){
            this.removeListViewItem(msgid);
            this.updateShowIsNull();
            this.mMsgIds = mo.userMessage.msgIds.slice(0);
        });

        this.updateShowIsNull();
    },

    onExit : function(){
        this._super();
        mo.eventHelper.remove(this);
    },

    onSecondClose : function(){
        mo.userMessage.getUserMessageIds();
        return this._super();
    },

    loadItemTemple : function(){
        var temple = mo.seekWidgetByName(this.ccsNode, "Panel_temple");
        this.ListView_content.setItemModel(temple);
        this.ListView_content.removeAllChildren();
    },

    updateListView : function(){
        //找到本地没有下载的
        var noLocalMsgids = [];

        //清除已经存在的
        this.ListView_content.removeAllChildren();

        for (var i = 0 ; i < this.mMsgIds.length; i++){
            this.addItem();
            var msgid = this.mMsgIds[i];
            var isExsit = mo.userMessage.existKey(msgid);
            if (isExsit){
                var userMessage = mo.userMessage.readUserMessage(msgid);
                this.setItemInfo(msgid, userMessage);
            }else{
                //从服务器拿
                noLocalMsgids.push(msgid);
            }
        }

        mo.gameMsgManager.sendUserMessage(noLocalMsgids);
    },

    addItem : function(){
        this.ListView_content.pushBackDefaultItem();
        var index = this.ListView_content.getItems().length - 1;
        var ui = this.ListView_content.getItem(index);
        var size = cc.size(this.ListView_content.getContentSize().width, 150);

        var Button_pick = mo.seekWidgetByName(ui, "Button_pick");
        Button_pick.setTag(index);

        ui.setContentSize(size);
        mo.seekWidgetByName(ui, "Image_bg").setContentSize(cc.size(size.width, 148));
        Button_pick.setPositionX(size.width - 60);
        mo.seekWidgetByName(ui, "FullText_prize").setPositionX(size.width - 200);

        var Button_copy = mo.seekWidgetByName(ui, "Button_copy");
        Button_copy.setPositionX(size.width - 200);
    },

    setItemInfo : function(msgid, userMessage){
        //ui
        var ui = this.getListViewItem(msgid);
        var json_params = JSON.parse(userMessage.json_params);

        //标题
        var Text_title = mo.seekWidgetByName(ui, "Text_title");
        mo.gameHelper.setTextString(Text_title, json_params.title);

        //时间
        var Text_time = mo.seekWidgetByName(ui, "Text_time");
        Text_time.setPositionX(Text_title.getPositionX() + Text_title.getContentSize().width + 10);
        mo.gameHelper.setTextString(Text_time, userMessage.msg_time);

        //内容
        mo.seekWidgetByName(ui, "FullText_content").setString(json_params.content);

        //按钮
        var Button_pick = mo.seekWidgetByName(ui, "Button_pick");

        //奖励文字
        var FullText_prize = mo.seekWidgetByName(ui, "FullText_prize");

        //是否是奖励
        if (userMessage.msg_type == 0){
            //文字
            this.setButtonTitle(Button_pick, "删除");
            FullText_prize.setString("");
            Button_pick.setTag(msgid);
            Button_pick.addTouchEventListener(this.onActionDel, this);
        }else{
            //奖励
            this.setButtonTitle(Button_pick, "领取");

            var price = "";
            var items = json_params.items.split(",");
            var items_num = json_params.items_num.split(",");
            for (var i = 0; i < items.length; i++){
                price += (mo.gameHelper.getItemIdForCHS(items[i]) + " X" + items_num[i]);
                if (i != items.length - 1){
                    price += "\n";
                }
            }
            FullText_prize.setString(price);
            Button_pick.setTag(msgid);
            Button_pick.addTouchEventListener(this.onActionPick, this);
        }

        //特殊处理
        if (json_params.title == "大红包"){
            var Button_copy = mo.seekWidgetByName(ui, "Button_copy");
            Button_copy.setVisible(true);

            Button_copy.addTouchEventListener(function(sender, type){
                if (mo.gameHelper.getIsTouchEnd(type)) {
                    var wxId = mo.userMessage.copyWxId;
                    mo.nativeHelper.copyString(wxId);
                    mo.showRoomMsgShow("复制成功");
                }
            }, this);
        }
    },

    setButtonTitle : function(button, title){
        var Text_text = mo.seekWidgetByName(button, "Text_text");
        mo.gameHelper.setTextString(Text_text, title);
    },

    updateShowIsNull : function(){
        var Text_tishi = mo.seekWidgetByName(this.ccsNode, "Text_tishi");
        if (this.mMsgIds.length == 0){
            Text_tishi.setVisible(true);
        }else{
            Text_tishi.setVisible(false);
        }
    },

    getListViewItem : function(msgid){
        var index = 0;
        for (var i = 0 ; i < this.mMsgIds.length; i++){
            if (this.mMsgIds[i] == msgid){
                index = i;
                break;
            }
        }
        var ui = this.ListView_content.getItem(index);
        return ui;
    },

    onActionPick : function(sender, type){
        if (mo.gameHelper.getIsTouchEnd(type)) {
            var index = sender.getTag();
            cc.log("领取 msgid:" + index);
            mo.gameMsgManager.sendUserMessageAction(index, 2);
        }
    },

    onActionDel : function(sender, type){
        if (mo.gameHelper.getIsTouchEnd(type)) {
            var index = sender.getTag();
            cc.log("删除 msgid:" + index);
            mo.gameMsgManager.sendUserMessageAction(index, 0);
        }
    },

    removeListViewItem : function(msgId){
        var index = -1;
        for (var i = 0; i < this.mMsgIds.length; i++){
            if (this.mMsgIds[i] == msgId){
                index = i;
                break;
            }
        }

        if (index != -1){
            this.mMsgIds.splice(index, 1);
            this.ListView_content.removeItem(index);
        }
    }
});